package fact.view.controller;

import java.io.Serializable;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ValueChangeEvent;
import javax.faces.model.SelectItem;
import javax.servlet.http.HttpSession;

import fact.view.DTO.NodeAttributeDTO;
import fact.view.DTO.NodeHierarchyDTO;
import fact.view.model.NodeHierarchyModelBean;
import fact.view.util.FacesUtils;
import fact.view.util.ServletUtil;

@ManagedBean(name = "nodeAttributeBean")
@ViewScoped
public class NodeAttributeBean extends ServletUtil implements Serializable 
{

	private static final long serialVersionUID = 8151655755631089776L;
	
	
	@ManagedProperty(value="#{nodeAttributeModelBean}")
	private NodeAttributeModelBean nodeAttributeModelBean;	
	
	@ManagedProperty(value="#{nodeHierarchyModelBean}")
	private NodeHierarchyModelBean nodeHierarchyModelBean;	
	
	
	
	ArrayList<NodeAttributeDTO> attributeList=new  ArrayList<NodeAttributeDTO>();
    HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
    
    private NodeAttributeDTO attribute;
	private SelectItem[] nodeSelectList;
	
	
	private int segmentId;
	private int nodeId;
    
	
	public String saveAttributes() throws SQLException
	{
	
		if(attributeList!=null)
		{
			nodeAttributeModelBean.editNodes(attributeList);
			FacesUtils.addInfoMessage(null,"Attribute Successfully Updated.");
		}
		return null;
	}
	
    public ArrayList<NodeAttributeDTO> getAttributeList() {
		
    	
		try{
			
			if(session.getAttribute("session_attributeList"+nodeId)==null)
			{
				attributeList=nodeAttributeModelBean.getAtributeList(nodeId);
				session.setAttribute("session_attributeList"+nodeId, attributeList);
			}
			else
				attributeList=(ArrayList<NodeAttributeDTO>)session.getAttribute("session_attributeList"+nodeId);
		   }
		catch(Exception ex)
		{
			ex.printStackTrace();
		}
		
		
		return attributeList;
	}

	public NodeAttributeModelBean getNodeAttributeModelBean() {
		return nodeAttributeModelBean;
	}

	public void setNodeAttributeModelBean(
			NodeAttributeModelBean nodeAttributeModelBean) {
		this.nodeAttributeModelBean = nodeAttributeModelBean;
	}

	
	public void toggleSegmentId(ValueChangeEvent e) 
	{
		try
		{
		this.segmentId=Integer.parseInt(e.getNewValue().toString());
		System.out.println("setting segmentId :"+segmentId);
		
		if(session.getAttribute("session_nodeListSegment"+segmentId)==null)
		{
			ArrayList<NodeHierarchyDTO> nList=nodeHierarchyModelBean.getKpiNodeList(segmentId);
			nodeSelectList = new SelectItem[nList.size()];
			 for(int i=0;i<nList.size();i++)
				 nodeSelectList[i] = new SelectItem(nList.get(i).getNodeId(), nList.get(i).getNodeName());
			 
			 session.setAttribute("session_nodeListSegment"+segmentId, nodeSelectList);


		}
		else
			nodeSelectList=(SelectItem[])session.getAttribute("session_nodeListSegment"+segmentId);
		}
		
		catch(Exception ex)
		{
			ex.printStackTrace();
		}
	}
	
	public void toggleNodeId(ValueChangeEvent e)
	{
		System.out.println("---------------==>>");
		System.out.println(segmentId);
		this.nodeId=Integer.parseInt(e.getNewValue().toString());
		getAttributeList();
		System.out.println(nodeId);
		
	}
	
	


	public SelectItem[] getNodeSelectList() {
		return nodeSelectList;
	}

	public void setNodeSelectList(SelectItem[] nodeSelectList) {
		this.nodeSelectList = nodeSelectList;
	}

	public int getSegmentId() {
		return segmentId;
	}

	public void setSegmentId(int segmentId) {
		this.segmentId = segmentId;
	}

	public int getNodeId() {
		return nodeId;
	}

	public void setNodeId(int nodeId) {
		this.nodeId = nodeId;
	}

	public HttpSession getSession() {
		return session;
	}

	public void setSession(HttpSession session) {
		this.session = session;
	}

	public NodeAttributeDTO getAttribute() {
		return attribute;
	}

	public void setAttribute(NodeAttributeDTO attribute) {
		this.attribute = attribute;
	}


	public void setAttributeList(ArrayList<NodeAttributeDTO> attributeList) {
		this.attributeList = attributeList;
	}

	public NodeHierarchyModelBean getNodeHierarchyModelBean() {
		return nodeHierarchyModelBean;
	}

	public void setNodeHierarchyModelBean(
			NodeHierarchyModelBean nodeHierarchyModelBean) {
		this.nodeHierarchyModelBean = nodeHierarchyModelBean;
	}


    
    
    
    
}
